package site.yaoer.dongbao.portal.web.advice;

import io.jsonwebtoken.JwtException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import site.yaoer.dongbao.ums.common.base.exception.TokenException;
import site.yaoer.dongbao.ums.common.base.result.ResultWrapper;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ArithmeticException.class)
    public ResultWrapper customException(){
        return ResultWrapper.builder().code(301).msg("异常").build();
    }


    /**
     * 自定义 token异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(JwtException.class)
    public ResultWrapper loginException(Exception e){
        return ResultWrapper.getFailBuilder().code(501).msg("Token 错误\n" + e.getMessage()).build();
    }

    @ExceptionHandler(SQLException.class)
    public ResultWrapper globalSQLException(Exception e){
        return ResultWrapper.getFailBuilder().code(501).msg(e.getMessage()).build();
    }

    @ExceptionHandler(Exception.class)
    public ResultWrapper globalException(Exception e){
        e.printStackTrace();
        return ResultWrapper.getFailBuilder().code(501).msg(e.getMessage()).build();
    }
}
